package listeners;

import java.awt.KeyEventDispatcher;
import java.awt.event.KeyEvent;

import enums.Direction;

import application.GameManager;
import application.auxclasses.Constant;
import application.snakes.Snake;

public class MyDispatcher implements KeyEventDispatcher {

	private Snake _snake;
	private int _count;
	
	public MyDispatcher(Snake snake){
		this._snake = snake;
		this._count = 0;
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent e) {
		System.out.println("The flag is " + Constant.get_flag());
		System.out.println("The counter is" + this._count);
		if ((Constant.get_flag())  || (this._count < 2)){
			if (e.getID() == KeyEvent.KEY_PRESSED) {
				System.out.println("Passed into KeyPressed!");
				if (e.getKeyCode() == KeyEvent.VK_LEFT){
					System.out.println("going to change snake direction " + "left");
					this._snake.changeDirection(Direction.WEST);
				}
				if (e.getKeyCode() == KeyEvent.VK_RIGHT){
					System.out.println("going to change snake direction " + "right");
					this._snake.changeDirection(Direction.EAST);
				}
				if (e.getKeyCode() == KeyEvent.VK_UP){
					System.out.println("going to change snake direction " + "up");
					this._snake.changeDirection(Direction.NORTH);
				}
				if (e.getKeyCode() == KeyEvent.VK_DOWN){
					System.out.println("going to change snake direction " + "down");
					this._snake.changeDirection(Direction.SOUTH);
				}
				Constant.set_flag(false);
				if (this._count < 2){
					this._count++;
				}
			} else if (e.getID() == KeyEvent.KEY_RELEASED) {
				GameManager.print("inside KEY_RELEASED");
			} else if (e.getID() == KeyEvent.KEY_TYPED) {
				GameManager.print("inside KEY_TYPED");
			}
			
		}
		return false;
	}

}
